/*
 * Copyright (c) 2024 iSoftStone Education Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdbool.h>
#include <math.h> // 使用 sqrt

#include "mpu6050.h"
#include "los_task.h"
#include "ohos_init.h"
#include "cmsis_os.h"
#include "config_network.h"
#include "smart_home.h"
#include "smart_home_event.h"
#include "smart_security.h"
#include "su_03t.h"
#include "iot.h"
#include "lcd.h"
#include "picture.h"
#include "adc_key.h"


extern bool sfbj;
#define ROUTE_SSID      "abcdefgh"          // WiFi账号
#define ROUTE_PASSWORD "abcdefgh"       // WiFi密码

#define MSG_QUEUE_LENGTH                                16
#define BUFFER_LEN                                      50



static unsigned int m_msg_queue;
unsigned int m_su03_msg_queue;

bool alarm_light_state = false;
bool beep_state = false;
bool auto_state = false;
/***************************************************************
 * 函数名称: iot_thread
 * 说    明: iot线程
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void iot_thread(void *args) {
  uint8_t mac_address[12] = {0x00, 0xdc, 0xb6, 0x90, 0x01, 0x00,0};

  char ssid[32]=ROUTE_SSID;
  char password[32]=ROUTE_PASSWORD;
  char mac_addr[32]={0};


  VendorSet(VENDOR_ID_WIFI_MODE, "STA", 3); // 配置为Wifi STA模式
  VendorSet(VENDOR_ID_MAC, mac_address, 6); // 多人同时做该实验，请修改各自不同的WiFi MAC地址
  VendorSet(VENDOR_ID_WIFI_ROUTE_SSID, ssid, sizeof(ssid));
  VendorSet(VENDOR_ID_WIFI_ROUTE_PASSWD, password,sizeof(password));

reconnect:
  SetWifiModeOff();
  int ret = SetWifiModeOn();
  if(ret != 0){
    printf("wifi connect failed,please check wifi config and the AP!\n");
    return;
  }
  mqtt_init();

  while (1) {
    if (!wait_message()) {
      goto reconnect;
    }
    LOS_Msleep(1);
  }
}


void smart_security_thread(void *arg)
{

}

/***************************************************************
 * 函数名称: device_read_thraed
 * 说    明: 设备读取线程
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void device_read_thraed(void *arg)
{
    double read_data[2] = {0};

    mq2_init();
    body_induction_dev_init();

    while(1)
    {
        mq2_read_data(&read_data[0]);
        body_induction_get_state((bool *)&read_data[1]);
        LOS_QueueWrite(m_msg_queue, (void *)&read_data, sizeof(read_data), LOS_WAIT_FOREVER);
        LOS_QueueWrite(m_su03_msg_queue, (void *)&read_data, sizeof(read_data), LOS_WAIT_FOREVER);
        LOS_Msleep(500);
printf("%f", read_data[0]);
        printf("++++");
printf("%f", read_data[1]);
        printf("++++");
    }
}
/***************************************************************
 * 函数名称: smart_home_thread
 * 说    明: 智慧家居主线程
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void smart_home_thread(void *arg)
{
    double *data_ptr = NULL;
double ppm_range = 800.0;
    double illumination_range = 50.0;
    double temperature_range = 35.0;
    double humidity_range = 80.0;
    bool light_last_state = false;
    bool beep_last_state = false;
    e_iot_data iot_data = {0};
        double read_data[2] = {0};
        short mpu6050_data[3] = {0};
    mpu6050_init();
    mq2_init();
    body_induction_dev_init();
    beep_dev_init();
    alarm_light_init();
    i2c_dev_init();
    lcd_dev_init();
    motor_dev_init();
    light_dev_init();
    su03t_init();

    // lcd_load_ui();
    lcd_show_ui();

    while(1)
    {
        mq2_read_data(&read_data[0]);
        body_induction_get_state((bool *)&read_data[1]);
        LOS_QueueWrite(m_msg_queue, (void *)&read_data, sizeof(read_data), LOS_WAIT_FOREVER);
        // LOS_QueueWrite(m_su03_msg_queue, (void *)&read_data, sizeof(read_data), LOS_WAIT_FOREVER);
        
        LOS_QueueRead(m_msg_queue, (void *)&data_ptr, BUFFER_LEN, LOS_WAIT_FOREVER);

      
        event_info_t event_info = {0};
        //等待事件触发,如有触发,则立即处理对应事件,如未等到,则执行默认的代码逻辑,更新屏幕
        int ret = smart_home_event_wait(&event_info,3000);
        if(ret == LOS_OK){
            //收到指令
            printf("event recv %d ,%d\n",event_info.event,event_info.data.iot_data);
            switch (event_info.event)
            {
                case event_key_press:
                    smart_home_key_process(event_info.data.key_no);
                    
                    break;
                case event_iot_cmd:
                    smart_home_iot_cmd_process(event_info.data.iot_data);
                    break;
                case event_su03t:
                    smart_home_su03t_cmd_process(event_info.data.su03t_data);
                    break;
               default:break;
            }

        }

        double temp,humi,lum;

        sht30_read_data(&temp,&humi);
        bh1750_read_data(&lum);
        mpu6050_read_data(mpu6050_data);

double total_acc = sqrt(
    (mpu6050_data[0] * 0.000061035 * 9.80665) * (mpu6050_data[0] * 0.000061035 * 9.80665) +
    (mpu6050_data[1] * 0.000061035 * 9.80665) * (mpu6050_data[1] * 0.000061035 * 9.80665) +
    (mpu6050_data[2] * 0.000061035 * 9.80665) * (mpu6050_data[2] * 0.000061035 * 9.80665))-1.1;
    char acc_str[10];


        lcd_set_illumination(lum);
        lcd_set_temperature(temp);
        lcd_set_humidity(humi);
        lcd_set_speed(total_acc);


        if (mqtt_is_connected()) 
        {
            
            // 发送iot数据
            iot_data.AccX = mpu6050_data[0];
            iot_data.AccY = mpu6050_data[1];
            iot_data.AccZ = mpu6050_data[2];
            iot_data.illumination = lum;
            iot_data.temperature = temp;
            iot_data.humidity = humi;
            iot_data.total_acc = total_acc;
            iot_data.light_state = get_light_state();
            iot_data.motor_state = get_motor_state();
            // iot_data.auto_state = auto_state;
            iot_data.smoke = data_ptr[0];

                if(sfbj){
                    lcd_set_krqt_state(true);
                }
                else{
                    if(data_ptr[0]>500){
                        lcd_set_bjd_state(true);
                    lcd_set_krqt_state(true);
                    alarm_light_set_gpio(true);
                    beep_set_state(true);
                    alarm_light_state = true;
                }else{
                    lcd_set_bjd_state(false);
                    lcd_set_krqt_state(false);
                    alarm_light_set_gpio(false);
                    beep_set_state(false);
                    alarm_light_state  = false;
                }
                }

                
            iot_data.body = (bool)data_ptr[1];
            iot_data.beep_state = beep_state;
            iot_data.alarm_light_state = alarm_light_state;
            iot_data.auto_state = auto_state;
            send_msg_to_mqtt(&iot_data);
                    printf("============= smart security example ==============\n");
        printf("======== data ========\r\n");
        printf("ppm:%5.2f\r\n", data_ptr[0]);
        printf("acc:%5.2f\r\n", total_acc);
        
        printf("body_induction:%d\r\n", (bool)data_ptr[1]);
        printf("======== state ========\r\n");
        printf("beep_state:%d\r\n", beep_state);

        printf("alarm_light_state:%d\r\n", alarm_light_state);
        printf("auto_state:%d\r\n", auto_state);

                printf("************ Mpu6050 Process ************\n");

        

        printf("x %d y %d z %d \r\n", mpu6050_data[0], mpu6050_data[1], mpu6050_data[2]);

        printf("\r\n");

        printf("\r\n");

            lcd_set_network_state(true);
        }else{  
            lcd_set_network_state(false);
        }

        lcd_show_ui();
    }
}



/***************************************************************
 * 函数名称: iot_smart_hone_example
 * 说    明: 开机自启动调用函数
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void zsxb_space_station()
{
    unsigned int thread_id_1;
    unsigned int thread_id_2;
    unsigned int thread_id_3;
        // unsigned int thread_id_4;
    // unsigned int thread_id_5;
    TSK_INIT_PARAM_S task_1 = {0};
    TSK_INIT_PARAM_S task_2 = {0};
    TSK_INIT_PARAM_S task_3 = {0};
    // TSK_INIT_PARAM_S task_4 = {0};  // 添加声明
// TSK_INIT_PARAM_S task_5 = {0};  // 添加声明
    unsigned int ret = LOS_OK;
    
    smart_home_event_init();


    ret = LOS_QueueCreate("queue", MSG_QUEUE_LENGTH, &m_msg_queue, 0, BUFFER_LEN);
    if (ret != LOS_OK)
    {
        printf("Falied to create Message Queue ret:0x%x\n", ret);
        return;
    }

    ret = LOS_QueueCreate("su03_queue", MSG_QUEUE_LENGTH, &m_su03_msg_queue, 0, BUFFER_LEN);
    if (ret != LOS_OK)
    {
        printf("Falied to create Message Queue ret:0x%x\n", ret);
        return;
    }

    task_1.pfnTaskEntry = (TSK_ENTRY_FUNC)smart_home_thread;
    task_1.uwStackSize = 2048;
    task_1.pcName = "smart hone thread";
    task_1.usTaskPrio = 24;
    
    ret = LOS_TaskCreate(&thread_id_1, &task_1);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }

    task_2.pfnTaskEntry = (TSK_ENTRY_FUNC)adc_key_thread;
    task_2.uwStackSize = 2048;
    task_2.pcName = "key thread";
    task_2.usTaskPrio = 24;
    ret = LOS_TaskCreate(&thread_id_2, &task_2);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }

    task_3.pfnTaskEntry = (TSK_ENTRY_FUNC)iot_thread;
    task_3.uwStackSize = 20480*5;
    task_3.pcName = "iot thread";
    task_3.usTaskPrio = 24;
    ret = LOS_TaskCreate(&thread_id_3, &task_3);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
}

APP_FEATURE_INIT(zsxb_space_station);
